﻿using System;
using System.Collections.Generic;
using System.Globalization;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using SubSonic;
using System.Linq;

namespace Medianamik.Client.Data.Repositories
{
    public interface IModuleRepository : IMedianamikRepository<MedianamikModule>, IMedianamikModuleRepository
    {

    }

    public class ModuleRepository : MedianamikRepository<MedianamikModuleCollection,
                                          MedianamikModule>, IModuleRepository
    {

        public ModuleRepository(ICultureService cultureService,
            IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public Core.MedianamikModuleDTO GetByAlias(string alias, CultureInfo culture)
        {
            var module = GetWithFilters(culture, ContentState.Draft, ActiveStatus.Activated, MedianamikTypes.MedianamikModule, null,
                new[] { new Where { ColumnName = "Alias", Comparison = Comparison.Equals, ParameterValue = alias } },
                 null, null, null, false).FirstOrDefault();

            return module == null ? null : ToMedianamikModule(module);
        }

        private Core.MedianamikModuleDTO ToMedianamikModule(MedianamikModule module)
        {
            return new Core.MedianamikModuleDTO
                       {
                           DisplayName = module.Name,
                           ID = module.NodeId,
                           Alias = module.AliasX,
                           Position = module.Position,
                           SearchResultPageId = module.SearchResultPage,
                           ShowInPublishing = module.ShowInPublishing.GetValueOrDefault()
                       };
        }

        //public IList<Core.MedianamikModuleDTO> GetAllModules(CultureInfo culture)
        //{
        //    return All().Select(ToMedianamikModule).ToList();
        //}
    }
}
